package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.dao.RoomDao;
import io.renren.entity.RoomEntity;
import io.renren.entity.RoomEslEntity;
import io.renren.entity.SysDictEntity;
import io.renren.param.SysDictQueryParam;
import io.renren.service.ApplicationService;
import io.renren.service.RoomEslService;
import io.renren.service.RoomService;
import io.renren.service.SysDictService;
import io.renren.vo.RoomEntityVo;
import io.renren.vo.RoomVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("roomService")
public class RoomServiceImpl extends ServiceImpl<RoomDao, RoomEntity> implements RoomService {
    @Autowired
    private ApplicationService applicationService;
    @Autowired
    private RoomDao roomDao;
    @Autowired
    private RoomEslService roomEslService;
    @Autowired
    private SysDictService sysDictService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String state = null, city = null, floor = null, name = null, bookDate = null, startTime = null, endTime = null, compId = null;
        state = (String) params.get("state");
        city = (String) params.get("city");
        floor = (String) params.get("floor");
        name = (String) params.get("name");
        compId = (String) params.get("compId");
        bookDate = (String) params.get("bookDate");
        startTime = (String) params.get("startTime");
        endTime = (String) params.get("endTime");
        List<Long> bookedRoomIds = new ArrayList<>();

        //已被预约的会议室
        if (StringUtils.isNotBlank(bookDate) && StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            bookedRoomIds = applicationService.queryBookedRoomIds(bookDate, startTime, endTime);
        }

        QueryWrapper<RoomEntity> roomEntityQueryWrapper = new QueryWrapper<RoomEntity>()
                .eq(state != null, "state", state)
                .eq(floor != null, "floor", floor)
                .eq(city != null, "city", city)
                .eq(name != null, "name", name)
                .eq(compId != null, "comp_id", compId)
                .notIn(bookedRoomIds.size() > 0, "id", bookedRoomIds);
        List<RoomVo> roomVos = new ArrayList<>();
        Map<String, String> timeMap = new HashMap<>();

        List<RoomEntity> rooms = roomDao.selectList(roomEntityQueryWrapper);

        for (RoomEntity room : rooms) {
            RoomVo roomVo = new RoomVo();
            RoomEntityVo roomEntityVo = new RoomEntityVo().convertFor(room);
            SysDictQueryParam param = new SysDictQueryParam();
            param.setName("floor");
            param.setCode(room.getFloor());
            param.setType(room.getCompId());
            SysDictEntity dictEntity = sysDictService.queryByNameCodeType(param);
            roomEntityVo.setFloor(dictEntity != null ? dictEntity.getValue() : room.getFloor());
            param.setName("roomState");
            param.setCode(room.getState());
            dictEntity = sysDictService.queryByNameCode(param);
            roomEntityVo.setState(dictEntity != null ? dictEntity.getValue() : room.getState());
            roomVo.setRoomEntityVo(new RoomEntityVo().convertFor(room));

            Map<String, Object> map = new HashMap<>();
            map.put("roomId", room.getId());
            map.put("limit", "1000");
            PageUtils roomEslPage = roomEslService.queryPage(map);
            List<RoomEslEntity> roomEslEntities = (List<RoomEslEntity>) roomEslPage.getList();
            List<Map<String, String>> maps = new ArrayList<>();
            for (RoomEslEntity roomEslEntity : roomEslEntities) {
                Map<String, String> eslMap = new HashMap<>();
                eslMap.put("type", roomEslEntity.getType());
                eslMap.put("eslId", roomEslEntity.getEslId());
                eslMap.put("alias", roomEslEntity.getAlias());
                maps.add(eslMap);
            }
            roomVo.setEslIds(maps);
            roomVos.add(roomVo);

        }

        return new PageUtils(roomVos, roomVos.size(), 100, 1);
    }

    public RoomEntity queryByCompIdAndId(String compId, Long id){
        return roomDao.selectOne(new QueryWrapper<RoomEntity>().eq("comp_id", compId)
        .eq("id", id));
    }

}
